# Definition for a binary tree node.
class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right

root=TreeNode(3,None,None)
root.left=TreeNode(9,None,None)
root.right=TreeNode(20,None,None)
root.right.left=TreeNode(15,None,None)
root.right.right=TreeNode(7,None,None)

def dfs(root,depth,res,maxDep):
    #出口条件
    if root.left==None and root.right==None:
        res.append(depth+1)
        if maxDep<depth+1:
            maxDep=depth+1
        return maxDep
    if root.left!=None and root.right!=None:
        maxDepL=dfs(root.left,depth+1,res,maxDep)
        maxDepR=dfs(root.right,depth+1,res,maxDep)
        maxDep=max(maxDepL,maxDepR,maxDep)
    if root.left!=None and root.right==None:
        maxDepL=dfs(root.left,depth+1,res,maxDep)
        maxDep=max(maxDep,maxDepL)
    if root.left==None and root.right!=None:
        maxDepR=dfs(root.right,depth+1,res,maxDep)
        maxDep=max(maxDep,maxDepR)

    return maxDep
def maxDepth(root):

    maxDept=dfs(root,0,[],0)
    print(maxDept)
maxDepth(root)